package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tongyi.lizhishop.beans.CardListBean;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.utils.PromptDialogUtils;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.common.BaseActivity;
import com.tongyi.lizhishop.managers.APIServiceManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.leefeng.promptlibrary.PromptDialog;

public class WithDrawActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.chooseBankCard)
    TextView chooseBankCard;
    @BindView(R.id.withDrawMoney)
    EditText withDrawMoney;
    @BindView(R.id.getAllMoneyBtn)
    TextView getAllMoneyBtn;
    @BindView(R.id.button_apply)
    Button buttonApply;
    @BindView(R.id.activity_with_draw)
    LinearLayout activityWithDraw;
    private AlertDialog alertDialog;
    private TextView rightTextView;
    private float moneyCount;
    private PromptDialog prompDialog;
    private String withDrawBankid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_draw);
        Bundle extras = getIntent().getExtras();
        moneyCount = extras.getFloat("moneyCount");
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "提现");
        rightTextView = titlebar.getRightTextView();
        rightTextView.setText("提现记录");
        rightTextView.setOnClickListener(this);
        initView();
    }

    private void initView() {
        withDrawMoney.setHint("当前可用余额" + moneyCount + "元");
        prompDialog = PromptDialogUtils.getPrompDialog(this);
    }


    @OnClick({R.id.getAllMoneyBtn, R.id.button_apply, R.id.chooseBankCard})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.getAllMoneyBtn:
                //全部提现
                withDrawMoney.setText(moneyCount + "");
                break;
            case R.id.button_apply:
                if (checkParams(withDrawMoney.getText().toString())) {
                    withDraw();
                }
                break;
            case R.id.chooseBankCard:
                getMyBankCardList();

                break;
        }
    }

    private boolean checkParams(String s) {
        if (TextUtils.isEmpty(withDrawBankid)) {
            ToastUtils.showLong("请先选择银行卡");
            return false;
        }

        try {
            float v = Float.parseFloat(s);
            if (v > moneyCount || v <= 0) {
                if (v > moneyCount) {
                    ToastUtils.showShort("你输得金额大于可用余额");

                } else {

                    ToastUtils.showShort("提现金额不能小于等于0");
                }
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    private void getMyBankCardList() {

        prompDialog.showLoading("请等待");
        APIServiceManager.getInstance().create(MessageApi.class)
                .showMyBank(SPUtils.getInstance().getString("userid"), 100000, 0)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CardListBean>() {
                    @Override
                    public void onSuccess(CardListBean value) {
                        prompDialog.dismiss();
                        if (value.getArray().size() == 0) {
                            //没有银行卡,跳转到添加卡包界面;
                            ToastUtils.showLong("请先添加银行卡");
                            ActivityUtils.startActivity(WithDrawActivity.this, AddIcCardActivity.class);
                        } else {
                            prompDialog.dismiss();
                            showDialog(value.getArray());
                        }

                        LogUtils.w(value);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        // multipleStatusView.showError();
                        prompDialog.showError("获取银行卡信息失败");
                    }
                });
    }

    private void withDraw() {
        // TODO: 18-1-1 接口没挂
        APIServiceManager.getInstance().create(MessageApi.class)
                .addBank(SPUtils.getInstance().getString("userid"), withDrawMoney.getText().toString(), withDrawBankid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if ("succ".equals(value.getRe())) {
                            prompDialog.showSuccess("提现成功", false);
                        } else {
                            prompDialog.showError(value.getMsg(), false);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        ToastUtils.showShort("无法联网!请重试");
                    }
                });


    }

    private void showDialog(final List<CardListBean.ArrayBean> array) {
        if (alertDialog == null) {
            View dialogView = getLayoutInflater().inflate(R.layout.choose_icard, null);
            final RadioGroup radioGroup = (RadioGroup) dialogView.findViewById(R.id.bankNameContainer);
            int size = array.size();
            for (int i = 0; i < size; i++) {
                final CardListBean.ArrayBean arrayBean = array.get(i);
                final RadioButton radioButton = createRadioButton(arrayBean.getName());
                radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        chooseBankCard.setText(arrayBean.getName());
                        withDrawBankid = arrayBean.getMban_id();
                        alertDialog.dismiss();
                    }
                });
                radioGroup.addView(radioButton);
            }
            alertDialog = new AlertDialog.Builder(this).setView(dialogView).show();
            alertDialog.getWindow().setWindowAnimations(R.style.dialog_anim);
        }
        alertDialog.show();
    }

    private RadioButton createRadioButton(String bankName) {
        RadioButton radioButton = new RadioButton(this);
        radioButton.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT));
        int size = SizeUtils.dp2px(getResources().getDimension(R.dimen.margin5dp));
        radioButton.setPadding(0, size, 0, size);
        radioButton.setText(bankName);
        return radioButton;
    }


    @Override
    public void onClick(View v) {
        if (v == rightTextView) {
            //添加卡片
            // ActivityUtils.startActivity(this, AddIcCardActivity.class);
            //提现记录
            ActivityUtils.startActivity(this, WithDrawRecordActivity.class);


        }
    }
}
